Skip to content

Change default serialization units - #313

Draft
kylecarow wants to merge 4 commits into
fastsim-3from
f3/unit-defaults
Draft

Change default serialization units#313
kylecarow wants to merge 4 commits into
fastsim-3from
f3/unit-defaults

Conversation

@kylecarow

Copy link
Copy Markdown
Collaborator

Built on #309

1. New canonical serialization units (proc macro: quantity_config)

The following quantities now serialize in practical engineering units instead of the SI base unit. The raw uom float is in the SI base, so each requires a new serialize_with helper to convert on write:

Quantity Before After
Energy joules kilojoules
Power watts kilowatts
Pressure pascals kilopascals
SpecificEnergy joules/kg kWh/kg
SpecificPower W/kg kW/kg
Temperature kelvin °C
TemperatureInterval kelvin °C

The Ratio "bare name legacy escape hatch" was also removed — Ratio fields now get the standard field_ratio / field_percent treatment like every other SI quantity.

2. New serialize_with helpers (serde_helpers.rs)

Seven new impl_si_serialize_as! expansions to support the above: energy_as_kilojoules, energy_as_kilowatt_hours, power_as_kilowatts, pressure_as_kilopascals, ratio_as_percent, specific_energy_as_kilowatt_hours_per_kilogram, specific_power_as_kilowatts_per_kilogram, temperature_as_degrees_celsius, temperature_interval_as_degrees_celsius.

3. Field-level #[si_unit(...)] overrides on struct fields

Across vehicle_model.rs, chassis.rs, conv.rs, hev.rs, hvac_*.rs, electric_machine.rs, fuel_converter.rs, fuel_storage.rs, transmission.rs, and reversible_energy_storage.rs — fields where the new global default isn't the right choice are overridden with an explicit #[si_unit(...)] annotation (e.g. #[si_unit(unitless)] for drag/rolling-resistance coefficients that should serialize as bare dimensionless ratios, #[si_unit(percent)] for SOC and efficiency fields).

Struct Field Type #[si_unit] Reason
Cycle grade Vec<si::Ratio> percent Grade is conventionally reported in %
CycleElement grade si::Ratio percent Matches Cycle.grade
Chassis drag_coef si::Ratio unitless Dimensionless aerodynamic coefficient (e.g. 0.3) — not a percentage
Chassis wheel_rr_coef si::Ratio unitless Rolling-resistance coefficient (e.g. 0.01) — dimensionless, not a percentage
Chassis wheel_fric_coef si::Ratio unitless Friction coefficient (e.g. 0.7) — dimensionless, not a percentage
ConventionalVehicle alt_eff si::Ratio percent Alternator efficiency, naturally expressed as %
RGWDBState soc_fc_on_buffer TrackedState<si::Ratio> percent SOC threshold expressed as %
RESGreedyWithDynamicBuffers speed_soc_disch_buffer_coeff Option<si::Ratio> percent SOC coefficient
RESGreedyWithDynamicBuffers speed_soc_fc_on_buffer_coeff Option<si::Ratio> percent SOC coefficient
RESGreedyWithDynamicBuffers speed_soc_regen_buffer_coeff Option<si::Ratio> percent SOC coefficient
HEVStopStartControl soc_fc_forced_on Option<si::Ratio> percent SOC threshold expressed as %
HVACSystemForLumpedCabinState cop TrackedState<Option<si::Ratio>> unitless Coefficient of performance (e.g. 2.5) — dimensionless ratio, not a percentage
HVACSystemForLumpedCabinAndRESState cop TrackedState<Option<si::Ratio>> unitless Same as above
ElectricMachineState eff TrackedState<si::Ratio> percent Efficiency
ElectricMachineState eff_fwd_at_max_input TrackedState<si::Ratio> percent Efficiency at peak input
ElectricMachineState eff_at_max_regen TrackedState<si::Ratio> percent Efficiency at max regen
FuelConverterState eff TrackedState<si::Ratio> percent Efficiency
FuelConverterThermalState eff_coeff TrackedState<si::Ratio> percent Thermal efficiency coefficient
FuelStorage energy_capacity si::Energy kilowatt_hours Overrides the new global Energy default (kJ) — kWh is more natural for fuel
ReversibleEnergyStorage energy_capacity si::Energy kilowatt_hours Battery capacity in kWh is universal convention
ReversibleEnergyStorage min_soc si::Ratio percent SOC limit
ReversibleEnergyStorage max_soc si::Ratio percent SOC limit
ReversibleEnergyStorageState soc TrackedState<si::Ratio> percent State of charge
ReversibleEnergyStorageState soc_regen_buffer TrackedState<si::Ratio> percent SOC buffer threshold
ReversibleEnergyStorageState soc_disch_buffer TrackedState<si::Ratio> percent SOC buffer threshold
ReversibleEnergyStorageState eff TrackedState<si::Ratio> percent Efficiency
ReversibleEnergyStorageState soh TrackedState<si::Ratio> percent State of health (type also promoted from TrackedState<f64>)
TransmissionState eff TrackedState<si::Ratio> percent Efficiency
VehicleState grade_curr TrackedState<si::Ratio> percent Current road grade

The core distinction between unitless and percent is physical meaning: dimensionless coefficients used in multiplication (drag, rolling resistance, friction, COP) serialize with their raw value and get unitless; fractions that represent a proportion of a maximum (efficiency, SOC, grade) serialize as percentages.

4. soh field promoted to si::Ratio

In ReversibleEnergyStorage, soh was a raw TrackedState<f64> — it's now TrackedState<si::Ratio> with #[si_unit(percent)], giving it the same dimensional-safety and multi-unit deserialization that all other SI fields have.

5. Integration test updates

tests/test_serde_api_integration.rs updated to match the new canonical key names (e.g. energy_capacity_kilowatt_hours instead of energy_capacity_joules, min_soc_percent instead of min_soc_ratio).

TODO: perhaps fields like friction/drag/rr coefficients should never have been ratios?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant